#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

int main(int argc, char**argv)
{
   int sockfd,n;
   struct sockaddr_in servaddr;//,cliaddr;
   char sendline[1000];
   char recvline[1000];

   if (argc != 2)
   {
      printf("usage:  client <IP address>\n");
      exit(1);
   }

   sockfd=socket(AF_INET,SOCK_STREAM,0);

   bzero(&servaddr,sizeof(servaddr));
   servaddr.sin_family = AF_INET;
   servaddr.sin_addr.s_addr=inet_addr(argv[1]);
   servaddr.sin_port=htons(32000);

   if (servaddr.sin_addr.s_addr == INADDR_NONE) {
      printf("error: invalid address\n");
      exit(-1);
   }

   if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr))) {
      printf("error: could not connect\n");
      exit(-1);
   }

*((int *) &sendline[0]) = htonl(1);
*((int *) &sendline[4]) = htonl(127);

printf("len: %d\n", strlen(sendline));

//   while (fgets(sendline, 10000,stdin) != NULL)
//   {
//      sendto(sockfd,sendline,strlen(sendline),0,
//             (struct sockaddr *)&servaddr,sizeof(servaddr));

      sendto(sockfd,sendline,8,0,
             (struct sockaddr *)&servaddr,sizeof(servaddr));

      n=recvfrom(sockfd,recvline,10000,0,NULL,NULL);
      recvline[n]=0;
      fputs(recvline,stdout);
//   }

    close(sockfd);

    return 0;
}
